using System;
namespace AGame.TabOrm.TabOrmAttributeExpand
{
    /// <summary>
    /// 枚举值转换
    /// </summary>
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
    public class EnumTabField : TabField
    {
        public EnumRecordType RecordType = EnumRecordType.Number;

        public enum EnumRecordType
        {
            Number,
            String
        }

        public override Func<string, object> GetStrToObjFunc(Type tabType, Type targetType)
        {
            switch (RecordType)
            {
                case EnumRecordType.Number:
                    return s => Enum.ToObject(targetType, int.TryParse(s, out var num) ? num : 0);
                case EnumRecordType.String:
                    return s => Enum.Parse(targetType, s);
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        public override Func<object, string> GetObjToStrFunc(Type tabType, Type targetType)
        {
            switch (RecordType)
            {
                case EnumRecordType.Number:
                    return o => ((int) o).ToString();
                case EnumRecordType.String:
                    return o => Enum.GetName(targetType, o);
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }
}